BI.namespace('sprite');

BI.sprite.ImageSprite = new Class({
	Extends : BI.sprite.BaseSprite,
	
	initialize : function(x, y, imageResource, config) {
		 this.parent(x, y, config);
		 this.imageResource = imageResource;
		 this.width = imageResource.image.width;  
		 this.height = imageResource.image.height;
		 console.log("width:" + this.width + ", height:" + this.height);
	},
	
	draw : function(context) {
		context.drawContext.drawImage(this.imageResource.image, this.x, this.y);
	},
	
	isTouched : function(x, y, context){
		if (x < this.x 
				|| x > (this.x + this.width)
				|| y < this.y
				|| y > (this.y + this.height)){
			return false;
		}
		
		return !this.isTransparentAtGlobalXY(x, y);
	},
	
	getPixelIndexFromGlobalXY: function(x, y){
		var local = this.globalToLocal(x, y);
		return (local.y * this.width + local.x) * 4; // each pixel is build from 4 positions Red[0], Green[1], Blue[2], Alpha[3]
	},
	
	isTransparentAtGlobalXY: function(x, y){
		var pixelIndex = this.getPixelIndexFromGlobalXY(x, y);
		return this.imageResource.data.data[pixelIndex + 3] == 0;   
	}
});